﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;

namespace TheOliver.Controls
{
    public class ResizeWindowBehavior : TargetedTriggerAction<FrameworkElement>
    {
        FrameworkElement element;

        protected override void OnAttached()
        {
            if (Application.Current.IsRunningOutOfBrowser)
            {
                element = this.AssociatedObject as FrameworkElement;
                element.MouseEnter += element_MouseEnter;
                element.MouseLeave += element_MouseLeave;
            }
            base.OnAttached();
        }

        void element_MouseLeave(object sender, MouseEventArgs e)
        {
            element.Cursor = Cursors.Arrow;
        }

        void element_MouseEnter(object sender, MouseEventArgs e)
        {
            switch (this.WindowResizeEdge)
            {
                case System.Windows.WindowResizeEdge.BottomLeft:
                    element.Cursor = Cursors.SizeNWSE;
                    break;

                case System.Windows.WindowResizeEdge.BottomRight:
                    element.Cursor = Cursors.SizeNWSE;
                    break;

                case System.Windows.WindowResizeEdge.TopLeft:
                    element.Cursor = Cursors.SizeNESW;
                    break;

                case System.Windows.WindowResizeEdge.TopRight:
                    element.Cursor = Cursors.SizeNESW;
                    break;

                case System.Windows.WindowResizeEdge.Right:
                case System.Windows.WindowResizeEdge.Left:
                    element.Cursor = Cursors.SizeWE;
                    break;

                case System.Windows.WindowResizeEdge.Top:
                case System.Windows.WindowResizeEdge.Bottom:
                    element.Cursor = Cursors.SizeNS;
                    break;
                default:
                    break;
            }
        }

        protected override void OnDetaching()
        {
            if (Application.Current.IsRunningOutOfBrowser)
            {
                element.MouseEnter -= element_MouseEnter;
            }
            base.OnDetaching();
        }

        protected override void Invoke(object o)
        {
            // Works only out-of-browser
            if (Application.Current.IsRunningOutOfBrowser
                && Application.Current.HasElevatedPermissions)
            {
                Application.Current.MainWindow.DragResize(WindowResizeEdge);
            }
        }

        public WindowResizeEdge WindowResizeEdge
        {
            get { return (WindowResizeEdge)GetValue(WindowResizeEdgeProperty); }
            set { SetValue(WindowResizeEdgeProperty, value); }
        }

        public static readonly DependencyProperty WindowResizeEdgeProperty =
            DependencyProperty.Register(
                "WindowResizeEdge",
                typeof(WindowResizeEdge),
                typeof(ResizeWindowBehavior),
                new PropertyMetadata(WindowResizeEdge.BottomRight));


    }
}
